package com.cldz.controller;

import com.cldz.common.dto.res.DeviceResDto;
import com.cldz.feign.DevListFeign;
import com.cldz.service.DevListService;
import com.cldz.response.support.ServerResponse;
import com.cldz.common.dto.req.DeviceDto;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @ Author     ：lixiao.
 * @ Date       ：Created in 15:33 2018/12/3
 * @ Description：设备列表接口
 * @ Modified By：
 */
@RestController
@Slf4j
public class DevListController implements DevListFeign {

    @Autowired
    private DevListService devListService;

    /**
     * 获取指定区域下设备列表
     * @param deviceDto
     * @return
     */
    @GetMapping("/getDevListByAreaID")
    public ServerResponse getDevListByAreaID(DeviceDto deviceDto){
        ServerResponse<List<DeviceResDto>> serverResponse = devListService.getDevListByAreaID(deviceDto);
        return serverResponse;
    }

    /**
     * 获取指定站点下设备列表
     * @param deviceDto
     * @return
     */
    @GetMapping("/getDevicesInfoWithStation")
    public ServerResponse getDevicesInfoWithStation(DeviceDto deviceDto){
        List<DeviceResDto> data = devListService.getDevicesInfoWithStation(deviceDto);
        Integer amount = devListService.getDevicesInfoWithStationAmount(deviceDto);
        return ServerResponse.createDataAndCount(amount,data);
    }

    /**
     * 获取指定设备信息
     * @param devID
     * @return
     */
    @GetMapping("/dev/{devID}")
    public ServerResponse<DeviceDto> getDevByID(@PathVariable(name = "devID")String devID){
        DeviceDto dev = devListService.getDevByID(devID);
        return ServerResponse.createDataList(dev);
    }

    /**
     * 获取指定类型设备列表
     * @param deviceDto
     * @return
     */
    @GetMapping("/getDevListByTypeID")
    public ServerResponse getDevListByTypeID(DeviceDto deviceDto){
        List<DeviceResDto> devListByTypeID = devListService.getDevListByTypeID(deviceDto);
        return ServerResponse.createDataList(devListByTypeID);
    }

    /**
     * 根据IP地址及端口获取设备
     * @return
     */
    @GetMapping("/getDevicesWithIpPort")
    public ServerResponse getPassiveDeviceByInfo(DeviceDto deviceDto){
        List<DeviceResDto> passiveDeviceByInfo = devListService.getPassiveDeviceByInfo(deviceDto);
        return ServerResponse.createDataList(passiveDeviceByInfo);
    }

    /**
     * 判断是否已存在相同IP、端口即地址的设备
     * @param deviceDto
     * @return
     */
    @GetMapping("/isExistDev")
    public ServerResponse isExistDev(DeviceDto deviceDto){
        boolean existDev = devListService.isExistDev(deviceDto);
        return ServerResponse.createResult(existDev);
    }

    /**
     * 获取设备类型列表
     * @param deviceDto
     * @return
     */
    @GetMapping("/deviceTypes")
    public ServerResponse getDeviceTypes(DeviceDto deviceDto){
        List<DeviceResDto> deviceTypes = devListService.getDeviceTypes(deviceDto);
        return ServerResponse.createDataList(deviceTypes);
    }

    /**
     * 设备列表按设备大类进行分类统计
      * @param deviceDto
     * @return
     */
    @GetMapping("/censusWithDeviceType")
    public ServerResponse censusWithDeviceType(DeviceDto deviceDto){
        String strDevIDs = deviceDto.getStrDevIDs();
        if(StringUtils.isNotBlank(strDevIDs)){
            strDevIDs = strDevIDs.replaceAll(",", "','");
            deviceDto.setStrDevIDs(strDevIDs);
        }
        List<Map<String,Object>> list = devListService.censusWithDeviceType(deviceDto);
        return ServerResponse.createDataList(list);
    }

    /**
     * 获取设备状态
     * @param deviceDto
     * @return
     */
    @GetMapping("/getDeviceStatus")
    public ServerResponse getDeviceStatus(DeviceDto deviceDto ){
        List<Map<String,Object>> data = devListService.getDeviceStatus(deviceDto);
        return ServerResponse.createDataList(data);
    }


}
